package com.duoshu.grj.sosoliuda.upload;

import android.content.Context;
import android.util.Log;

import com.alibaba.sdk.android.vod.upload.VODSVideoUploadClient;
import com.alibaba.sdk.android.vod.upload.VODSVideoUploadClientImpl;
import com.alibaba.sdk.android.vod.upload.model.SvideoInfo;
import com.alibaba.sdk.android.vod.upload.session.VodHttpClientConfig;
import com.alibaba.sdk.android.vod.upload.session.VodSessionCreateInfo;

import java.io.File;

/**
 * 阿里短视频上传
 */

public class SnapVideoUploadAliImp implements SnapVideoUploadApi {
    private Context mContext;

    private  AuthInfo authInfo;//阿里云视频上传授权信息
    private String videoPath = "/sdcard/1.mp4";//视频路径
    private String imagePath = "/sdcard/1.png";//封面图片路径

    private VODSVideoUploadClient vodsVideoUploadClient;
    private VodSessionCreateInfo vodSessionCreateInfo;

    private SnapVideoUploadCallback callback;
    private SnapVideoUploader snapVideoUploader = new SnapVideoUploader() {
        @Override
        public void onSTSTokenExpried() {
            //STS token过期之后刷新STStoken，如正在上传将会断点续传
            super.onSTSTokenExpried();
            Log.d("snapvideo", "onSTSTokenExpried");
            vodsVideoUploadClient.refreshSTSToken(authInfo.accessKeyId, authInfo.accessKeySecret, authInfo.securityToken, authInfo.expriedTime);
        }

        @Override
        public void onUploadRetry(String code, String message) {
            //上传重试的提醒
            Log.d("snapvideo", "onUploadRetry" + "code" + code + "message" + message);
            onUploadFailed("300", "onUploadRetry----");
            vodsVideoUploadClient.cancel();
        }

        @Override
        public void onUploadRetryResume() {
            //上传重试成功的回调.告知用户重试成功
            Log.d("snapvideo", "onUploadRetryResume");
        }

    };

    public SnapVideoUploadAliImp(String imgPath, String videoPath) {
        this.videoPath = videoPath;
        this.imagePath = imgPath;

    }

    @Override
    public void init(Context context) throws InvalidAuthInfoException {
        if (authInfo == null) {
            Log.d("video", "SnapVideoUploadAliImp.init--初始化上传类--未获取token---->");
            throw new InvalidAuthInfoException("100", "短视频上传未从服务器获取授权信息!");
        }
        if (mContext == null) {
            Log.d("video", "SnapVideoUploadAliImp.init--初始化上传类--已获取token--" + authInfo.toString() + "---->");
            mContext = context;
            checkParameter();
            initUploadParam();
        }
    }

    @Override
    public void setAuthInfo(AuthInfo authInfo) {
        this.authInfo = authInfo;
    }

    @Override
    public void upload(SnapVideoUploadCallback callback) {
        if (vodsVideoUploadClient == null)
            throw new RuntimeException("call init() first");

        if (this.callback == null) {
            this.callback = callback;
            snapVideoUploader.setCallback(callback);
        }

        vodsVideoUploadClient.uploadWithVideoAndImg(vodSessionCreateInfo, snapVideoUploader);

    }

    private void initUploadParam() {
        //参数请确保存在，如不存在SDK内部将会直接将错误throw Exception
        // 文件路径保证存在之外因为Android 6.0之后需要动态获取权限，请开发者自行实现获取"文件读写权限".
        VodHttpClientConfig vodHttpClientConfig = new VodHttpClientConfig.Builder()
                .setMaxRetryCount(2)
                .setConnectionTimeout(8 * 1000)
                .setSocketTimeout(8 * 1000)
                .build();

        //构建短视频VideoInfo,常见的描述，标题，详情都可以设置
        SvideoInfo svideoInfo = new SvideoInfo();
        svideoInfo.setTitle(new File(videoPath).getName());
        svideoInfo.setDesc("");
        svideoInfo.setCateId(1);

        //构建点播上传参数(重要)
        vodSessionCreateInfo = new VodSessionCreateInfo.Builder()
                .setImagePath(imagePath)
                .setVideoPath(videoPath)
                .setAccessKeyId(authInfo.accessKeyId)
                .setAccessKeySecret(authInfo.accessKeySecret)
                .setSecurityToken(authInfo.securityToken)
//                .setRequestID(authInfo.requestID)
                .setExpriedTime(authInfo.expriedTime)
                .setIsTranscode(true)
                .setSvideoInfo(svideoInfo)
                .setVodHttpClientConfig(vodHttpClientConfig)
                .build();

        vodsVideoUploadClient = new VODSVideoUploadClientImpl(mContext);
        vodsVideoUploadClient.init();
    }

    @Override
    public void resume() {
        vodsVideoUploadClient.resume();
    }

    @Override
    public void pause() {
        vodsVideoUploadClient.pause();
    }

    @Override
    public void cancel() {
        vodsVideoUploadClient.cancel();
    }


    private void checkParameter() {
       /* if(StringUtil.isEmpty(accessKeyId)) {
            return;
        } else if(StringUtil.isEmpty(accessKeySecret)) {
            Toast.makeText(mContext,"The specified parameter \"accessKeySecret\" cannot be null",Toast.LENGTH_LONG).show();
            return;
        } else if(StringUtil.isEmpty(securityToken)) {
            Toast.makeText(mContext,"The specified parameter \"securityToken\" cannot be null",Toast.LENGTH_LONG).show();
            return;
        } else if(StringUtil.isEmpty(expriedTime)) {
            Toast.makeText(mContext,"The specified parameter \"expriedTime\" cannot be null",Toast.LENGTH_LONG).show();
            return;
        } else if(!(new File(videoPath)).exists()) {
            Toast.makeText(mContext,"The specified parameter \"videoPath\" file not exists",Toast.LENGTH_LONG).show();
            return;
        } else if(!(new File(imagePath)).exists()) {
            Toast.makeText(mContext,"The specified parameter \"imagePath\" file not exists",Toast.LENGTH_LONG).show();
            return;
        }*/
    }

    @Override
    public void release() {
        vodsVideoUploadClient.release();
    }
}
